﻿#region MyJScript - LGPL Copyright (c) 2008 Lionel Laské
//
// This file is part of MyJScript.
//
// MyJScript is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// MyJScript is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with MyJScript; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
//================================================================================
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace MyJScript.Runtime
{
    /// <summary>
    /// Class for a MyJScript object.
    /// </summary>
    public class MJSObject
    {
        /// <summary>
        ///  Variables instance and methods instance
        /// </summary>
        Dictionary<string, object> members;

        /// <summary>
        /// Constructor
        /// </summary>
        public MJSObject()
        {
            this.members = new Dictionary<string, object>();
        }

        /// <summary>
        /// Check if object has this member.
        /// </summary>
        /// <param name="name">member's name</param>
        /// <returns>true if this member exist</returns>
        public bool HasMember(string name)
        {
            return members.ContainsKey(name);
        }

        /// <summary>
        /// Set a member value.
        /// </summary>
        /// <param name="name">member name</param>
        /// <param name="value">new value</param>
        public virtual void Set(string name, object value)
        {
            // Remove it first if need
            if (members.ContainsKey(name))
                members.Remove(name);

            // Add property
            members.Add(name, value);
        }

        /// <summary>
        /// Get a member value.
        /// </summary>
        /// <param name="name">member name</param>
        /// <returns>member value</returns>
        public virtual object Get(string name)
        {
            if (!members.ContainsKey(name))
                return null;

            return members[name];
        }

        /// <summary>
        /// Convert to string.
        /// </summary>
        /// <returns>Object as string</returns>
        public override string ToString()
        {
            // Print member value
            bool first = true;
            StringBuilder res = new StringBuilder("{");
            foreach (KeyValuePair<string, object> member in members)
            {
                if (!first)
                    res.Append(", ");
                res.Append(member.Key);
                res.Append(":");
                if (member.Value == this)
                    res.Append("this");
                else
                    res.Append(member.Value.ToString());
                first = false;
            }
            res.Append("}");

            return res.ToString();
        }
    }
}
